#pragma once
#include "DJointGroup.h"
#include "../DWorld.h"
#include "../DVector3.h"
#include "DJoint.h"
#include "../DOde.h"

namespace Skill
{
	namespace Ode
	{
		namespace Joints
		{
			void DJointGroup::AddJoint(DJoint^ j)
			{
				if(!_Joints->Contains(j))
					_Joints->Add(j);
			}
			void DJointGroup::RemoveJoint(DJoint^ j)
			{				
				_Joints->Remove(j);
			}

			DJointGroup::!DJointGroup()
			{					
				if(_Free && _This && DOde::IsInitialized)
					dJointGroupDestroy(_This);
				_Free = false;
				_This = nullptr;

			}

			void DJointGroup::CollectManagedMemory()
			{
				if(_Joints)
				{
					for each (DJoint^ j in _Joints)
					{						
						j->_Free = false;
						j->_Container = nullptr;
					}				
					_Joints->Clear();
				}
				_Joints = nullptr;					
			}
			void DJointGroup::Empty()
			{
				for each (DJoint^ j in _Joints)
				{					
					j->_Free = false;
					j->_Container = nullptr;
				}
				dJointGroupEmpty(_This);
				_Joints->Clear();
			}
		}
	}
}